<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 文章管理 iframe 框</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <input type="hidden" name="trainId" lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;margin-right: 5px">*</span>培训标题</label>
        <div class="layui-input-inline">
            <input type="text" name="trainContent" lay-verify="required" readonly placeholder="请输入培训标题" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">不可修改</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;margin-right: 5px">*</span>开始-结束</label>
        <div class="layui-inline">
            <input type="text" name="trainStart" style="width: 460px" class="layui-input" id="test-laydate-format-range2" placeholder="开始时间 - 结束时间">
            <div class="layui-form-mid layui-word-aux">(开始时间不能是过去的时间)</div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;margin-right: 5px">*</span>考试时长</label>
        <div class="layui-input-inline">
            <input type="text" name="trainTime" lay-verify="required" placeholder="请输入考试时长" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">(考试时长不能大于60分钟)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;margin-right: 5px">*</span>题目数量</label>
        <div class="layui-input-inline">
            <input type="text" name="trainNum" lay-verify="required" placeholder="请输入题目数量" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">(题目数量不能大于50)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;margin-right: 5px">*</span>合格分数</label>
        <div class="layui-input-inline">
            <input type="text" name="passNum" lay-verify="required" placeholder="请输入合格分数" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">(合格分数不能大于题目数量)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color: red;margin-right: 5px">*</span>所选题库</label>
        <div class="" id="trainBanksDiv">
        </div>
    </div>

    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        version: true,
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function(){
        var $ = layui.$
            ,form = layui.form;

        initTrainBanks($,form);

        //回显数据
        var editData = parent.editData;
        var trainStart = editData.trainStart.replace("T"," ");
        var trainEnd = editData.trainEnd.replace("T"," ");
        console.log(editData);
        $('[name=trainId]').val(editData.trainId);
        $('[name=trainStart]').val(trainStart+" 到 "+trainEnd);
        $('[name=trainContent]').val(editData.trainContent);
        $('[name=trainTime]').val(editData.trainTime);
        $('[name=trainNum]').val(editData.trainNum);
        $('[name=passNum]').val(editData.passNum);
        $('[name=trainBanks]').each(function () {
           var trainBank = editData.trainBanks.split(",");
           for(var i in trainBank){
               if($(this).val() == trainBank[i]){
                   $(this).attr("checked",true);
               }
           }
        });
        form.render();

        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            var split = field['trainStart'].split("到");
            var trainBanks = "";
            $("input[name=trainBanks]:checked").each(function () {
                trainBanks += ","+$(this).val()
            });
            var dateStart = new Date($.trim(split[0]).replace(/-/g, '/'));
            var dateEnd = new Date($.trim(split[1]).replace(/-/g, '/'));
            field['trainStart'] = $.trim(split[0]);
            field['trainEnd'] = $.trim(split[1]);
            field['trainBanks'] = trainBanks.substr(1);
            console.log(field);

            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                url:"/admin/train/train",
                method:"put",
                data:field,
                dataType:"json",
                async:false,
                success:function(obj){
                    if(obj.code == 0){
                        parent.layui.table.reload('table-train'); //重载表格
                        parent.layer.close(index); //再执行关闭
                        parent.layer.msg("修改成功");
                    }else{
                        layer.msg(obj.msg);
                    }
                }
            });
        });
    });
    layui.use(['index', 'laydate'],function () {
        var $ = layui.$
            ,laydate = layui.laydate;

        laydate.render({
            elem: '#test-laydate-format-range2'
            ,type: 'datetime'
            ,range: '到'
            ,format: 'yyyy-MM-dd HH:mm:ss'
        });
    });

    function initTrainBanks($,form){
        $.ajax({
            url:"/admin/questionBank/expertBanksNum",
            method:"get",
            dataType:"json",
            async:false,
            success:function(obj){
                var html = "";
                for(var i in obj){
                    html += '<input type="checkbox" name="trainBanks" title="'+obj[i].bankName+' ('+obj[i].questionNum+'条)" value="'+obj[i].bankId+'"><input id="NUM_'+obj[i].bankId+'" type="hidden" value="'+obj[i].questionNum+'">';
                }
                $("#trainBanksDiv").append(html);
                form.render();
            }
        });
    }
</script>
</body>
</html>